﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Design
{
    public partial class ColorSwatch : GraphicsDeviceControl
    {
        private Texture2D transparentTile;
        private Texture2D blank;
        private SpriteBatch spriteBatch;
        private Color color;

        public ColorSwatch()
        {
            InitializeComponent();
            this.color = Color.White;
        }

        public Color GetColor()
        {
            return this.color;
        }

        [DefaultValue(255)]
        public byte A
        {
            get
            {
                return this.color.A;
            }
            set
            {
                this.color.A = value;
                this.Invalidate();
            }
        }

        [DefaultValue(255)]
        public byte R
        {
            get
            {
                return this.color.R;
            }
            set
            {
                this.color.R = value;
                this.Invalidate();
            }
        }

        [DefaultValue(255)]
        public byte G
        {
            get
            {
                return this.color.G;
            }
            set
            {
                this.color.G = value;
                this.Invalidate();
            }
        }

        [DefaultValue(255)]
        public byte B
        {
            get
            {
                return this.color.B;
            }
            set
            {
                this.color.B = value;
                this.Invalidate();
            }
        }


        protected override void Draw()
        {
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            int x = (this.Size.Width + this.transparentTile.Width - 1) / this.transparentTile.Width;
            int y = (this.Size.Height + this.transparentTile.Height - 1) / this.transparentTile.Height;

            this.spriteBatch.Begin();
            try
            {
                for (int i = 0; i < x; ++i)
                {
                    for (int j = 0; j < y; ++j)
                    {
                        this.spriteBatch.Draw(this.transparentTile, new Vector2(i * this.transparentTile.Width, j * this.transparentTile.Height), Color.White);
                    }
                }
                this.spriteBatch.Draw(this.blank, new Rectangle(0, 0, this.Width, this.Height), this.GetColor());
            }
            finally
            {
                this.spriteBatch.End();
            }
        }

        protected override void Initialize()
        {
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            this.blank = new Texture2D(this.GraphicsDevice, 1, 1);
            this.blank.SetData<Color>(new Color[] { Color.White });
            Stream backgroundTileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Ferpect.ComponentModel.Design.transparentHatch.png");
            this.transparentTile = Texture2D.FromStream(this.GraphicsDevice, backgroundTileStream);
        }

        public override string ToString()
        {
            return String.Format("R:{0} G:{1} B:{2} A:{3}", R, G, B, A);
        }
    }
}
